﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ActiveUp.Net.Mail;


namespace TractRecord.Service
{
    public partial class MailPurge : TractRecordPage
    {
        int purgedEmails = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            Code.Common.Log("Purge Mail Routine Starting.", "Purge Mail", 0, 0, System.Diagnostics.TraceEventType.Information, "Purge Mail");

            try
            {
                RunCheck();
            }
            catch (Exception ex)
            {
                Code.Common.Log(ex.ToString(), ex.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Critical, "Upload File Error");
            }
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            Code.Common.Log("Purge Mail Routine Completing.", "Purge Mail", 0, 0, System.Diagnostics.TraceEventType.Information, "Purge Mail Routine Completed");

            Code.Common.Log(purgedEmails + ": purged emails.", "Purge Mail", 0, 0, System.Diagnostics.TraceEventType.Information, "Purged Emails");
        }

        private void RunCheck()
        {
            switch (ConfigurationManager.AppSettings["type"])
            {
                case "IMAP":
                    Imap(Convert.ToBoolean(ConfigurationManager.AppSettings["ssl"]));

                    break;
                case "POP":
                    Pop(Convert.ToBoolean(ConfigurationManager.AppSettings["ssl"]));
                    break;
            }
        }

        private void Imap(Boolean Ssl)
        {
            Imap4Client p = new Imap4Client();
            if (Ssl)
            {
                p.ConnectSsl(ConfigurationManager.AppSettings["host"], Convert.ToInt32(ConfigurationManager.AppSettings["port"]));
            }
            else
            {
                p.Connect(ConfigurationManager.AppSettings["host"], Convert.ToInt32(ConfigurationManager.AppSettings["port"]));
            }

            p.Login(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["userpassword"]);

            Mailbox archives;

            try
            {
                archives = p.SelectMailbox("archives");
            }
            catch (Exception)
            {
                archives = p.CreateMailbox("archives");
            }

            if (archives.MessageCount > 0)
            {
                //clean up archive

                int days = Convert.ToInt32(ConfigurationManager.AppSettings["archivepurge"]);

                DateTime datebefore = DateTime.Now.AddDays(-1 * days);
                
                MessageCollection messages = archives.SearchParse("SEARCH SENTBEFORE " + datebefore.ToString("d-MMM-yyyy"));
                purgedEmails = messages.Count;
                
                foreach (Message message in messages)
                {
                    archives.DeleteMessage(message.IndexOnServer, true);
                }
                
            }
            else
            {
                Code.Common.Log("No emails to purge.", "Purge Mail", 0, 0, System.Diagnostics.TraceEventType.Information, "Purge Mail");
            }

            Code.DataAccess.AutoPurge(Convert.ToInt32(ConfigurationManager.AppSettings["activetoarchive"]),
                Convert.ToInt32(ConfigurationManager.AppSettings["archivetodeleted"]),
                Convert.ToInt32(ConfigurationManager.AppSettings["deletedtopurge"]));

        }

        private void Pop(Boolean Ssl)
        {
            Pop3Client p = new Pop3Client();

            if (Ssl)
            {
                p.ConnectSsl(ConfigurationManager.AppSettings["host"],
                    Convert.ToInt32(ConfigurationManager.AppSettings["port"]),
                    ConfigurationManager.AppSettings["username"],
                    ConfigurationManager.AppSettings["userpassword"]);
            }
            else
            {
                p.Connect(ConfigurationManager.AppSettings["host"],
                    Convert.ToInt32(ConfigurationManager.AppSettings["port"]),
                    ConfigurationManager.AppSettings["username"],
                    ConfigurationManager.AppSettings["userpassword"]);
            }
        }
    }
}